{
open Grammar

let in_for=ref false 
}

let newline = '\n' | '\r' '\n'
let letter= ['a'-'z' 'A'-'Z']
let digit = ['0'-'9']
let nombre= '-'? digit+
let ope = (letter+ | nombre) ('+' | '-') (letter+ | nombre )

rule token = parse
  | newline         { Lexing.new_line lexbuf; token lexbuf }
  | [' ' '\t'] +    { token lexbuf }
  | "//"            { comment lexbuf }
  | "("             { [LPAREN] }
  | ")"             { if !in_for then in_for:=not(!in_for); [RPAREN] }
  | "{"             { [LCURLY] }
  | "}"             { [RCURLY] }
  | "="             { [EQUAL] }
  | ","             { [COMMA] }
  | "#include"      { includeName lexbuf }
  | "print"         { [PRINT] }
  | "printAddrRam"     { [PRINTADDRRAM]}
  | "printAddrRom"     { [PRINTADDRROM]}
  | "for"           {in_for:=true;[FOR]}
  | "true"          {[BOOL true]}
  | "false"         {[BOOL false]}
  | nombre as n     { [ENTIER (int_of_string n)]}
  | '0'             { if !in_for then [ENTIER 0] else [BOOL false] }
  | '1'             { if !in_for then [ENTIER 1] else [BOOL true] }

  | (letter | digit | ['''])* "[" (letter+ | digit+ | ope) "]" as id {[NAME id]}

  | (digit+ as n)  ((letter | digit)+ as id) {fil n (Lexing.from_string(id))}
  | (letter | digit | [''' '.']) * as id   {[NAME id]}  

  | eof             { [EOF] }

and fil n = parse
  |(letter | digit)+ as id
      {
	let buf = ref [] in
	let t=int_of_string(n) in 
	  for k=0 to (t-1) do
	    buf:=(NAME (n^id^"["^string_of_int(k)^"]"))::(!buf);
	    if k<(t-1) then 
	      buf:=COMMA::(!buf);()
	  done;
	  List.rev (!buf)
      }

and comment = parse
  | newline {token lexbuf}
  | _       {comment lexbuf}
  | eof     { [EOF] }

and includeName = parse
  | newline         { Lexing.new_line lexbuf; includeName lexbuf }
  | [' ' '\t'] +    { includeName lexbuf }
  |  letter+ '.' letter+ as name {[INCLUDE name]}
